package commands;

import java.io.IOException;

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import util.Utils;
import vo.Photo;


public class PostPhotoCommand implements Comando {

	@Override
	public String execute(String[] args) throws IOException{
		if (args.length != 4) {
			return getSintaxe();
		}
		HTable table = new HTable(Photo.TABLENAME);
		Photo photo = new Photo();
		String[] array = {args[1]};
		byte[] photoId = photo.gerarId(array);

		Put put = new Put(photoId);
		byte[] attribute  = Utils.createStringInBytes(Photo.CF_ATTRIBUTES);
		byte[] link  = Utils.createStringInBytes(Photo.PHOTO_LINK);
		byte[] titulo  = Utils.createStringInBytes(Photo.PHOTO_TITLE);
		
		put.add(attribute, link, Bytes.toBytes(args[2]));
		put.add(attribute, titulo, Bytes.toBytes(args[3]));
		
		table.put(put);	
		
		String id = new String(photoId);
		return id;

	}

	@Override
	public String getKey() {
		return "-postPhoto";
	}

	@Override
	public String getSintaxe() {
		return "-postPhoto userId link title";
	}

}
